$(function() {

	if ($("#coverImage").length > 0) {
		var coverImage = $('#coverImageUrl').val();

		if (coverImage == null || coverImage == '') {
			// 初始化封面图片
			$("#coverImage").fileinput({
				uploadUrl : "/faxsun-info/ueditor/dispatch?action=uploadimage", // server
				// upload
				// action
				uploadAsync : true,
				minFileCount : 1,
				maxFileCount : 1
			});
		} else {
			$("#coverImage")
					.fileinput(
							{
								uploadUrl : "/faxsun-info/ueditor/dispatch?action=uploadimage", // server
								// upload
								// action
								uploadAsync : true,
								minFileCount : 1,
								maxFileCount : 1,
								initialPreview : [
										"<img src='"
												+ coverImage
												+ "' class='file-preview-image' alt='cover' title='cover'>", ],
							});
		}
	}

	$('#coverImage').on('fileuploaded',
			function(event, data, previewId, index, jqXHR) {
				if (data.response.state = 'SUCCESS') {
					$('#coverImageUrl').val(data.response.url);
				}
			});

	$('#coverImage').on('filecleared', function(event, key) {
		$('#coverImageUrl').val("");
	});

	// checkbox 选中事件
	$('#eventTable').on('check.bs.table check-all.bs.table',
			function(event, rows) {
				// 选中频道设置isSelected为true
				postSelectedItems(rows, true);
			});

	// checkbox 选中事件
	$('#eventTable').on('uncheck.bs.table uncheck-all.bs.table',
			function(event,rows) {
				// 选中频道设置isSelected为true
				console.log(rows);
				postSelectedItems(rows, false);
			});

	function postSelectedItems(rows, selected) {
		var content = "ids=" + getIdSelections(rows) + "&selected=" + selected;
		console.log(content);
		$.ajax({
			url : "/faxsun-info/admin/live/setselected",
			method : "POST",
			data : content
		}).done(function(data) {
			//alert(data.message);
			
			//TODO: 提示设置成功
		});
	}

	function getIdSelections(rows) {
		if (rows instanceof Array) {
			var idMap = $.map(rows,
					function(row) {
						return row.id
					});
			return idMap.join(',');
		} else {
			return rows.id;
		}
	}
});